package com.fafi.rxjava1.operator;

import com.fafi.rxjava1.Action1;
import com.fafi.rxjava1.Calling;
import com.fafi.rxjava1.Receiver;
import com.fafi.rxjava1.observeon.OperatorCallbackOn;
import com.fafi.rxjava1.subscribeon.OperatorCallOn;
import com.fafi.rxjava1.subscribeon.Switcher;
import com.fafi.rxjava1.transformer.Converter;

public class Caller<T> {
    final OnCall<T> onCall;

    public Caller(OnCall<T> onCall) {
        this.onCall = onCall;
    }

    public static <T> Caller<T> create(OnCall<T> onCall) {
        return new Caller<>(onCall);
    }

    public Calling call(Receiver<T> receiver) {
        this.onCall.call(receiver);
        return receiver;
    }

    public final <R> Caller<R> lift(final Operator<R, T> operator) {
        return create(new OnCallLift<>(onCall, operator));
    }

    public final <R> Caller<R> map(Func1<T, R> func) {
        return lift(new MapOperator<T, R>(func));
    }

    public interface OnCall<T> extends Action1<Receiver<T>> {

    }

    /**
     * 变换receiver
     * @param <R>
     * @param <T>
     */
    public interface Operator<R, T> extends Func1<Receiver<R>, Receiver<T>> {

    }

    public final com.fafi.rxjava1.operator.Caller<T> callOn(Switcher switcher) {
        return create(new OperatorCallOn<>(switcher, this));
    }

    public final com.fafi.rxjava1.operator.Caller<T> callBackOn(Switcher switcher) {
        return lift(new OperatorCallbackOn<>(switcher));
    }

    public final <R> Caller<R> unify(Converter<T, R> converter) {
        return converter.call(this);
    }
}
